-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.c
More file actions
33 lines (30 loc) · 763 Bytes
/
Solution.c
File metadata and controls
33 lines (30 loc) · 763 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
void printPattern(int n) {
int num = 1;
for(int i = 1; i <= n; i++) {
// Print leading spaces
for(int space = 1; space <= n - i; space++) {
printf(" ");
}
// Print numbers
if(i % 2 == 1) { // Odd rows: left to right
for(int j = 1; j <= n; j++) {
printf("%2d ", num++);
}
} else { // Even rows: right to left
int temp = num + n - 1;
for(int j = 1; j <= n; j++) {
printf("%2d ", temp--);
}
num += n;
}
printf("\n");
}
}
int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
printPattern(n);
return 0;
}